﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xuanren_FrontEnd.Model;

namespace Xuanren_FrontEnd.Areas.Back.Controllers
{
    public class ImgController : Controller
    {
        private XuanrenTherapyEntities db = new XuanrenTherapyEntities();
        // GET: Img
        public ActionResult Index(int id)
        {
            Session["pid"] = id;
            ViewBag.Img = id;
            return View();
        }

        public ActionResult Search(int page, int limit, string product_id)
        {
             
            {
                var list = db.Img.Where(p => true);
                if (!string.IsNullOrWhiteSpace(product_id))
                {
                    var pid = Convert.ToInt32(product_id);
                    list = list.Where(x => x.product_id == pid);
                }
                var count = list.Count();
                var datalist = list.OrderBy(x => x.product_id).Skip((page - 1) * limit).Take(limit).Select(p => new
                {
                    id = p.id,
                    image = p.image,
                    product_id = p.product_id,
                    create_user = p.create_user,
                    create_time = p.create_time.ToString(),
                    update_user = p.update_user,
                    update_time = p.update_time.ToString(),


                }).ToList();

                return Json(new { code = 0, msg = "", data = datalist, count = count });
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public ActionResult Add()
        {
            object pid= Session["pid"];
            ViewBag.pid = pid;
            return View();
        }

        public ActionResult DealAdd(Img img)
        {
            {
                string username = Session["username"].ToString();

                img.create_user = username;
                img.create_time= DateTime.Now;
                img.update_time= DateTime.Now;
                img.update_user = username;
                db.Img.Add(img);
                int r = db.SaveChanges();
                if (r > 0)
                {
                    Session.Remove("pid");
                    return Json(new { code = 0, msg = "新增成功" });
                }
                else
                {
                    return Json(new { code = -1, msg = "新增失败" });

                }
            }

        }
        [HttpPost]
        public ActionResult Delete(int id)
        {
            var obj = db.Img.FirstOrDefault(x => x.id == id);
            if (obj != null)
            {
                db.Img.Remove(obj);//删除该新闻
            }
            int r = db.SaveChanges();
            if (r > 0)
            {
                return Json(new { scu = true, msg = "删除成功" });
            }
            else
            {
                return Json(new { scu = true, msg = "删除失败" });

            }
        }
    }
}